using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

namespace Microsoft.Practices.Repository.Configuration.Design
{
    /// <summary>
    /// Represents the design manager for the Respository Factory.
    /// </summary>
    public class RepositoryFactoryConfigurationDesignManager : ConfigurationDesignManager
    {
        private const string repositoryFactorySectionName = "repositoryFactory";

        /// <summary>
        /// Initialize a new instance of the <see cref="RepositoryFactoryConfigurationDesignManager"/> class.
        /// </summary>
        public RepositoryFactoryConfigurationDesignManager()
        {
        }

        /// <summary>
        /// Registers the commands for the configurationnodes to be used in the designtime.
        /// </summary>
        /// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
        public override void Register(IServiceProvider serviceProvider)
        {
            RepositoryFactoryCommandRegistrar registrar = new RepositoryFactoryCommandRegistrar(serviceProvider);
            registrar.Register();
        }

        /// <summary>
        /// Gets the a <see cref="ConfigurationSectionInfo"/> for the repositoryFactory section.
        /// </summary>
        /// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
        /// <returns>A <see cref="ConfigurationSectionInfo"/> for the configuration for the configuration sources.</returns>
        protected override ConfigurationSectionInfo GetConfigurationSectionInfo(IServiceProvider serviceProvider)
        {
            ConfigurationNode rootNode = ServiceHelper.GetCurrentRootNode(serviceProvider);
            RepositoryFactorySectionNode node = null;
            if (null != rootNode) node = rootNode.Hierarchy.FindNodeByType(rootNode, typeof(RepositoryFactorySectionNode)) as RepositoryFactorySectionNode;
            RepositoryFactorySection section = null;
            if (node == null)
            {
                section = null;
            }
            else
            {
                RepositoryFactorySectionBuilder builder =
                    new RepositoryFactorySectionBuilder(serviceProvider, node);
                section = builder.Build();
            }
            string protectionProviderName = GetProtectionProviderName(node);

            return new ConfigurationSectionInfo(node, section, repositoryFactorySectionName, protectionProviderName);
        }

        /// <summary>
        /// Opens the repositoryFactory section, builds the design time nodes and adds them to the application node.
        /// </summary>
        /// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
        /// <param name="rootNode">The root node of the application.</param>
        /// <param name="section">The <see cref="ConfigurationSection"/> that was opened from the <see cref="IConfigurationSource"/>.</param>
        protected override void OpenCore(IServiceProvider serviceProvider, ConfigurationApplicationNode rootNode, ConfigurationSection section)
        {
            if (null != section)
            {
                RepositoryFactorySectionNodeBuilder builder = new RepositoryFactorySectionNodeBuilder(serviceProvider, (RepositoryFactorySection)section);
                RepositoryFactorySectionNode node = builder.Build();
                SetProtectionProvider(section, node);

                rootNode.AddNode(node);
            }
        }
    }
}
